package xman.mjava.thread;

import java.util.concurrent.atomic.AtomicBoolean;

public class RaceCondition {
    private static Object lock = new Object();
    
    private static volatile boolean firstInvoke = true;
    
    private static volatile AtomicBoolean firstSMPInvoke = new AtomicBoolean(true);
    
    public static void main(String[] args) {
        // default
        synchronized (lock) {
            if (firstInvoke) {
                invoke();
                firstInvoke = false;
            }
        }
        // DCL(double check lock)
        if (firstInvoke) {
            synchronized (lock) {
                if (firstInvoke) {
                    invoke();
                    firstInvoke = false;
                }
            }
        }
        // SMP simple
        if (firstSMPInvoke.getAndSet(false)) {
            
        }
        // SMP
        for (;;) {
            Boolean current = firstSMPInvoke.get();
            if (!current) {
                break;
            }
            if (firstSMPInvoke.compareAndSet(current, false)) {
                invoke();
                break;
            }
        }
    }
    
    public static void invoke() {
        
    }
}
